Conditions | 7 |
Paths | 32 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
33 | Server.prototype.request = function (options, btn) { |
||
34 | var ajaxOptions = { |
||
35 | url: '', |
||
36 | type: 'GET', |
||
37 | dataType: 'json', |
||
38 | data: {}, |
||
39 | async: true, |
||
40 | contentType: false, |
||
41 | cache: false, |
||
42 | }; |
||
43 | for (var key in options) { |
||
44 | ajaxOptions[key] = options[key]; |
||
45 | } |
||
46 | if (options.url && options.url.indexOf(inji.options.appRoot) !== 0) { |
||
47 | ajaxOptions.url = inji.options.appRoot + (options.url.replace(/^\//g, '')); |
||
48 | } |
||
49 | if (typeof btn != 'undefined') { |
||
50 | $(btn).data('loading-text', 'подождите'); |
||
51 | var btn = $(btn).button().button('loading'); |
||
52 | } |
||
53 | var callback = null; |
||
54 | if (typeof options.success != 'undefined') { |
||
55 | callback = options.success; |
||
56 | } |
||
57 | ajaxOptions.success = function (data, textStatus, jqXHR) { |
||
58 | if (typeof btn != 'undefined') { |
||
59 | btn.button('reset'); |
||
60 | } |
||
61 | if (ajaxOptions.dataType != 'json') { |
||
62 | callback(data, textStatus, jqXHR); |
||
63 | } else { |
||
64 | if (data.success) { |
||
65 | if (data.successMsg) { |
||
66 | noty({text: data.successMsg, type: 'success', timeout: 3500, layout: 'center'}); |
||
67 | } |
||
68 | if (typeof data.scripts == 'object') { |
||
69 | inji.loaded = false; |
||
70 | inji.onLoad(function () { |
||
71 | inji.Server.runCommands(data.commands); |
||
72 | if (callback !== null) { |
||
73 | callback(data.content, textStatus, jqXHR) |
||
74 | } |
||
75 | }); |
||
76 | if (data.scripts.length > 0) { |
||
77 | inji.loadScripts(data.scripts, 0); |
||
78 | } else { |
||
79 | inji.startCallbacks(); |
||
80 | } |
||
81 | } else { |
||
82 | inji.Server.runCommands(data.commands); |
||
83 | if (callback !== null) { |
||
84 | callback(data.content, textStatus, jqXHR); |
||
85 | } |
||
86 | } |
||
87 | } else { |
||
88 | inji.Server.runCommands(data.commands); |
||
89 | noty({text: data.error, type: 'warning', timeout: 3500, layout: 'center'}); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | var errorCallback = null; |
||
94 | if (typeof options.error != 'undefined') { |
||
95 | errorCallback = options.error; |
||
96 | } |
||
97 | ajaxOptions.error = function (jqXHR, textStatus, errorThrown) { |
||
98 | if (typeof btn != 'undefined') { |
||
99 | btn.button('reset'); |
||
100 | } |
||
101 | if (errorCallback != null) { |
||
102 | errorCallback(jqXHR, textStatus, errorThrown); |
||
103 | } else if (textStatus != 'abort') { |
||
104 | noty({text: 'Во время запроса произошла ошибка: ' + textStatus, type: 'warning', timeout: 3500, layout: 'center'}); |
||
105 | } |
||
106 | } |
||
107 | return $.ajax(ajaxOptions); |
||
108 | }; |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: